package com.main.common.interceptors;

import com.main.common.enums.SaTokenKey;
import com.main.common.utils.SaTokenUtil;
import com.main.common.utils.common.SpringUtils;
import jakarta.servlet.ServletException;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.LocaleResolver;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.i18n.LocaleChangeInterceptor;
import org.springframework.web.servlet.i18n.SessionLocaleResolver;

import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.Locale;
import java.util.TimeZone;

@Slf4j
@Component
public class LocaleInterceptor extends LocaleChangeInterceptor {

    @Autowired
    private LocaleResolver localeResolver =  SpringUtils.getBean(LocaleResolver.class);
    @Autowired
    private SessionLocaleResolver sessionLocaleResolver=  SpringUtils.getBean(SessionLocaleResolver.class);;

    private final SaTokenUtil saTokenUtil = new SaTokenUtil();

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws ServletException {
        System.out.println(LocalDateTime.now());
        System.out.println("LocaleInterceptor");
        // 在控制器执行之前执行一些操作
        System.out.println("------------------LocaleInterceptor--------------------");
        String lang = request.getParameter("lang");
        String timeZone = request.getParameter("timeZone");
        if(lang == null || lang.isEmpty()) {
            return true;
        }
        try {
            Locale locale = Locale.of(lang.split("_")[0], lang.split("_")[1]);
            localeResolver.setLocale(request,response,locale);
            sessionLocaleResolver.setDefaultLocale(locale);
            saTokenUtil.setBySessionKey(SaTokenKey.SESSION_LOCALE,locale);
            log.info("设置语言成功,{}", locale);
        }catch (Exception e){
            log.info("设置语言失败,{}", e.getMessage());
        }
        if(timeZone == null || timeZone.isEmpty()) {
            return true;
        }
        try {
            sessionLocaleResolver.setDefaultTimeZone(TimeZone.getTimeZone(ZoneId.of(timeZone)));
            saTokenUtil.setBySessionKey(SaTokenKey.SESSION_TIME_ZONE, ZoneId.of(timeZone));
            log.info("设置时区成功,{}", timeZone);
        }catch (Exception e){
            log.info("设置时区失败,{}", e.getMessage());
        }
        return true;
    }

    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
        System.out.println(LocalDateTime.now());
        System.out.println("LocaleInterceptor");
        // 在控制器执行之后，视图渲染之前执行一些操作
        System.out.println("------------------LocaleInterceptor--------------------");
        System.out.println(request);
    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
        // 在整个请求结束之后执行一些操作
        System.out.println("LocaleInterceptor after completion...");
    }

}

